﻿
var fs = require('fs');
var Promise = require('bluebird');

Promise.promisifyAll(fs)

var readFileAsync = function (path)
{
    //返回一个Promise对象，初始状态pending
    return new Promise(function (fulfill, reject)
    {
        fs.readFile(path, 'utf8', function (err, content)
        {
            if (err) return reject(err)
            return fulfill(content)
        })
    })
}

fs.readFileAsync('./TestPromise.js', 'utf8').then(function (content1)
{
    console.log('#', content1);
    //这里返回一个Promise对象
    return fs.readFileAsync('./TestJoose.js', 'utf8');

}).then(function (content2)
{
    console.log('##', content2);

}).catch(function(reason)
{
    console.log('Handle rejected promise (' + reason + ') here.');
});
